const query = require('../config/db.config.js')

const resuMod = {
  // 查看处理进度
  resuStatusMod:async (id)=>{
    let sql =
    `SELECT status FROM getresume WHERE _id = ${id}`
    const res = await query(sql)
    return res
  },
  // 投递状态
  sendReStatuMod:async (params)=>{
    let { reciverId, senderId,projId } = params
    let sql =
    `SELECT * FROM getresume WHERE reciverId = "${reciverId}" AND senderId = "${senderId}" AND projId = "${projId}"`
    const res = await query(sql)
    return res.length
  },
  // 删除投递
  deleteDeliverMod:async (id)=>{
    let sql = 
    `DELETE FROM 
      getresume 
    WHERE 
      _id = ${id} ;`
    await query(sql)
  },
  // 更新简历状态
  updateResuMod: async (params)=>{
    let { _id, status, reason } = params
    let occ =''
    let sql = 
     `UPDATE getresume 
      SET
        status = ${status},
        rejectReason = "${reason || occ}"
      WHERE 
        _id = ${_id}`
      await query(sql)
  },
  // 简历投递
  sendResumeMod: async (params)=>{
    let {projId, reciverId, senderId, contact} = params
    let sql = 
    `INSERT INTO getresume
      (projId, reciverId, senderId )
    VALUES
      ("${projId}","${reciverId}","${senderId}")`
    await query(sql)
    let contactSql = 
    `UPDATE userinfo SET contact =" ${contact}" WHERE userId = "${senderId}" `
    await query(contactSql)
  },
  // 收到的投递
  receResuMod: async (userId) => {
    let sql = 
    `SELECT
    pr.num,
    pr.projName,
    us.avatarUrl,
    us.nickName,
    us.userId,
    gr._id,
    gr.status,
    gr.rejectReason,
    gr.createTime
  FROM
    userinfo us,
    getresume gr,
    projinfo pr
  WHERE
    us.userId = gr.senderId 
    AND pr.projId = gr.projId
    AND gr.reciverId = "${userId}"
    ORDER BY createTime DESC
    `
    // 多对多查询
    const res = await query(sql)

    return res
  },
  deliverResuMod:async (userId) => {
    // 返回的select的数据
    let sql = 
    `SELECT
    pr.num,
    pr.projName,
    pr.projId,
    gr._id,
    gr.status,
    gr.createTime
  FROM
    getresume gr,
    projinfo pr
  WHERE
    pr.projId = gr.projId
    AND gr.senderId = "${userId}"
    ORDER BY createTime DESC`
    const res = await query(sql)
    return res
  },
} 

module.exports = resuMod